package chamans.away
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.AnimatedBitmapMaterial;
	
	import com.bulletflash.dynamics.*;
	
	import flash.geom.*;
	
	public class Ecran
	{
		private var BASE:Object;
		private var intern:Mesh;
		private var end:Function;
		private var Pos:Vector3D;
		private var mat:AnimatedBitmapMaterial;
		private var m_liste:Vector.<Mesh>;
		private var r_liste:Vector.<btRigidBody>;
		private var def:Array=[38, 7, 4, 43, 17, 20.5, 21.5];
		private var comp:Boolean
		private var tmp:Array;
		
		public function Ecran(base:Object, END:Function=null, P:Vector3D=null, Complex:Boolean=true)
		{
			BASE=base; end=END || new Function(); comp=Complex; 
			BASE.LOAD.IMGLIST("assets/demobullet/ecran/", ["ecran_s", "ecran_n" ], initMaterials , ".jpg");
		}
		
		private function initMaterials( A:Array  ) : void
		{
			tmp=A;
			BASE.LOAD.SWF("assets/demobullet/ecran/screen.swf", ["screen"], initMaterials0);
		}
		private function initMaterials0( A:Array  ) : void
		{
			mat = new AnimatedBitmapMaterial(new A[0](), true, true)
			mat.ambientColor = BASE.FogColor;
			mat.specularMap = tmp[0];
			mat.normalMap = tmp[1];
			mat.lights = BASE.LIGHT;
			
			mat.specular = 1.2;
			mat.ambient=.2;
			mat.gloss = 50;
			mat.name="screen"
			if(BASE.reflect)BASE.F_reflect(mat, .2)
			if(BASE.fog)BASE.F_Fog(mat)
			BASE.MatListe.push(mat);
			m_liste = new Vector.<Mesh>();
			r_liste = new Vector.<btRigidBody>();
			BASE.OBJ3D('assets/demobullet/ecran/ecran.obj', Define)
		}
		
		private function Define(obj:ObjectContainer3D) : void
		{
			intern = obj.getChildAt(0) as Mesh;
			BASE.Pause(end);end=null;
		}
		public function clone(p:Vector3D=null, rot:Vector3D=null ) : void
		{
			var mesh:Mesh = intern.clone() as Mesh;
			if(p)Pos=p; else Pos=RanPos();
			if(BASE.phyOn) Physics(mesh, rot ); 
			else {mesh.position=Pos; }
			
			mesh.material=mat;
			BASE.view.scene.addChild(mesh);
		}
		private function RanPos() : Vector3D
		{
			return new Vector3D(BASE.TOOL.Ran(1000, -1000),26, BASE.TOOL.Ran(1000));
		}
		public function Delete() : void
		{   
			if(BASE.phyOn) BASE.physics.Delete( m_liste, r_liste);
		}
		private function Physics(mesh:Mesh, r:Vector3D) : void
		{
			var phyData:Vector3D=new Vector3D(1, .6, .6);
			mesh=BASE.physics.Make("box", m_liste, r_liste, Pos, new Vector3D( 70,52,22 ), phyData, mesh, false, r);
		}
		
	}}
